#This is the script we have used in the Appendix of our paper in RAC.
#Its main purpose is to show reader the effect of changing the number
#of clusters on the outputs arbitrarily. Before using this code please
#be aware you need to run previously the code we have developed for the manuscript.

#Step 1: running K-means clustering with an arbitrary K = 5

#We added command lines before and after the "fviz_nbclust" function. Those lines allow
#readers to save the figures in high definition in their devices. We used the .tiff
#format because it provides a good balance between the file size and the image quality.

km.ConsumerApp <- kmeans(ConsumerScaleSample, 5, nstart = 30)

#Step 2: plotting the output from K-Means clustering with K = 5 

tiff("Figure1A.tiff", family = "Times New Roman", pointsize=10, width=2800, height=2000, res=600)
fviz_cluster(km.ConsumerApp, data = ConsumerScaleSample, palette = "jco",
             geom = "point", ellipse.type = "norm",
             repel = TRUE, ggtheme = theme_minimal())
dev.off()

#Step 3: running the K-Means clustering with PAM and an arbitrary K = 5

#We have used the "pam()" function to run the K-Means clustering. We have stored the output 
#in the object "pam.ConsumerApp". 

pam.ConsumerApp <- pam(ConsumerScaleSample, 5) 

#Step 4: plotting the output

#We plotted the output by using the "fviz_cluster()" function. 

tiff("Figure2A.tiff", family = "Times New Roman", pointsize=10, width=2800, height=2000, res=600)
fviz_cluster(pam.ConsumerApp, data = ConsumerScaleSample, palette = "jco",
        geom = "point", ellipse.type = "norm", repel = TRUE, ggtheme = theme_minimal()) 
dev.off()

#Step 5: initiating the DBSCAN

#Our first procedure for running the DBSCAN was to identify the eps. To do it, we plotted the 
#output for an arbitrary K = 5

tiff("Figure3A.tiff", family = "Times New Roman", pointsize=10, width=2800, height=2000, res=600)
dbscan::kNNdistplot(ConsumerScaleSample, k = 5)
abline(h = 2.87, lty = 2)
dev.off()

#Step 6: running DBSCAN with and arbitrary K = 5 and eps = 2.87

#We have run DBSCAN with those parameters and stored the output in the object "db.Consumer". 

db.ConsumerApp <- fpc::dbscan(ConsumerScaleSample, eps = 2.87, MinPts = 3) 

#Step 7: plotting the output from DBSCAN

#We have plotted the output with the function "fviz_cluster()".

tiff("Figure4A.tiff", family = "Times New Roman", pointsize=10, width=2800, height=2000, res=600)
fviz_cluster(db.ConsumerApp, data = ConsumerScaleSample, stand = FALSE, ellipse.type = "norm",
        show.clust.cent = FALSE, geom = "point", palette = "jco", ggtheme = theme_classic())
dev.off()

#Step 8: identifying the outliers in DBSCAN

#We have used the following command to identify the outliers after running the DBSCAN

db.Consumer$cluster

#Step 9: running the FCM clustering with an arbitrary K = 5.

#We have run the latest clustering technique (FCM) by using the "fanny()" function and storing
#the output in the object "fuz.Consumer". We have printed the output with the function "print()".

fuz.ConsumerApp <- fanny(ConsumerScaleSample, 5) 
print(fuz.ConsumerApp)

#Step 10: plotting the output

#We have plotted the output from FCM by using the "fviz_cluster()" function.

tiff("Figure5A.tiff", family = "Times New Roman", pointsize=10, width=2800, height=2000, res=600)
fviz_cluster(fuz.ConsumerApp, ellipse.type = "norm", repel = TRUE, palette = "jco", 
             geom = "point", ggtheme = theme_minimal(), legend = "right")
dev.off()
